const express=require("express")
const app=express()
const dbPool=require("../config/dbPool")
const request =require("request")
const formidable = require("formidable")
const fs=require("fs")
const path = require("path")

const movieController={
    getMovieDate(req,resp){
        dbPool.connect("select * from movies",[],(err,data)=>{
            if(!err){
                if(data.length>0){
                   start=(req.query.currentPage-1)*req.query.pagesize
                   last= req.query.currentPage*req.query.pagesize
                   newArr=data.slice(start,last)
                   allTotal=data.length
                   endArr=[newArr,allTotal]
                   resp.send(endArr)
                
                }
            }
   })
         
    },
    getCommentDate(req,resp){
        dbPool.connect("select * from `comment`",[],(err,data)=>{
            if(!err){
                if(data.length>0){
                   start=(req.query.currentPage-1)*req.query.pagesize
                   last= req.query.currentPage*req.query.pagesize
                   newArr=data.slice(start,last)
                   allTotal=data.length
                   endArr=[newArr,allTotal]
                   resp.send(endArr)
                
                }
            }
   })    

    },
    deletesMovie(req,resp)
    { 
        dbPool.connect("DELETE FROM movies WHERE id =?",[req.query.id],(err,data)=>{
            if(!err){
               resp.send("删除成功")
            }
 })

         
    }
       ,
       deletesComment(req,resp){
        dbPool.connect("DELETE FROM `comment` WHERE id =?",[req.query.id],(err,data)=>{
            if(!err){
               resp.send("删除成功")
            }
 })
       }
       ,
       updateMovie(req,resp){
        dbPool.connect("UPDATE movies SET `title`=?,`type`=?,`videoSrc`=?  WHERE id=?",[req.query.title,req.query.type,req.query.videoSrc,req.query.id],(err,data)=>{
            if(!err){
                resp.send("修改成功")
            }
})


         
       } ,
       searchMovie(req,resp){
    //   console.log(req.query.title)
        if(req.query.title){
            dbPool.connect("select * from movies where title=?",[req.query.title],(err,data)=>{
                if(!err){
                   if(data.length>0){
                       resp.send(data)
                   }else{
                     resp.send(data)
                   }
                }
            })
         } else if(req.query.title==""){
               dbPool.connect("select * from movies",[],(err,data)=>{
                     if(!err){
                         resp.send(data)
                     }
               })
             
         }

         
       } ,
       searchComment(req,resp){
        if(req.query.title){
            dbPool.connect("select * from `comment` where `name`=?",[req.query.title],(err,data)=>{
                if(!err){
                   if(data.length>0){
                       resp.send(data)
                   }else{
                     resp.send(data)
                   }
                }
            })
         } else if(req.query.title==""){
               dbPool.connect("select * from `comment`",[],(err,data)=>{
                     if(!err){
                         resp.send(data)
                     }
               })
             
         }
        
         
       },
       //评分
       getM(req,resp){
        //    console.log(req.query.title)
          dbPool.connect("select * from movies where title=?",[req.query.title],(err,data)=>{
                          if(!err){
                            if(data.length>0){
                                  resp.send(data)
                            }    
                            
                          }
          })
       },
       addRate(req,resp){
        //    console.log(req.query)
        dbPool.connect("select * from rate where userId=? and movieId=?",[req.query.userId,req.query.movieId],(err,data)=>{
                                      if(!err){
                                         if(data.length>0){
                                          dbPool.connect("UPDATE rate SET `rateValue`=?  WHERE id=?",[req.query.rateValue,data[0].id],(err,data)=>{
                                                   if(!err){
                                                       resp.send("ok")
                                                   }
                                          })   
                                         }else{
                                             dbPool.connect("INSERT INTO rate(`userId`,`movieId`,`rateValue`) VALUES(?,?,?)",[req.query.userId,req.query.movieId,req.query.rateValue],(err,data)=>{
                                                     if(!err){
                                                      resp.send("ok")
                                                         
                                                     }
                                             })
                                         } 
                                         
                                      }
        })
          
       },
       getRate(req,resp){
        // console.log(req.query.userId)
        // console.log(req.query.movieId)
           dbPool.connect("select * from rate where `userId`=? and `movieId`=?",[req.query.userId,req.query.movieId],(err,data)=>{
                                        if(!err){
                                             if(data.length>0){
                                             resp.send(data)     
                                                 
                                             }else{
                                                 console.log("没得")
                                             }
                                        }else{
                                             console.log(err)
                                        }
           })
          
       },
       //轮播图
      getBannerDate(req,resp){
        dbPool.connect("select * from `banner`",[],(err,data)=>{
            if(!err){
                if(data.length>0){
                   start=(req.query.currentPage-1)*req.query.pagesize
                   last= req.query.currentPage*req.query.pagesize
                   newArr=data.slice(start,last)
                   allTotal=data.length
                   endArr=[newArr,allTotal]
                   resp.send(endArr)
                
                }
            }
   })    


       },
       searchBanner(req,resp){
        if(req.query.title){
            dbPool.connect("select * from `banner` where `name`=?",[req.query.title],(err,data)=>{
                if(!err){
                   if(data.length>0){
                       resp.send(data)
                   }else{
                     resp.send(data)
                   }
                }
            })
         } else if(req.query.title==""){
               dbPool.connect("select * from `banner`",[],(err,data)=>{
                     if(!err){
                         resp.send(data)
                     }
               })
             
         }
         
       },
       deleteBanner(req,resp){
        dbPool.connect("DELETE FROM `banner` WHERE id =?",[req.query.id],(err,data)=>{
            if(!err){
               resp.send("删除成功")
            }
 })
   
         
       },
       Blood(req,resp){
        dbPool.connect("select * from movies where type=? ",["热血"],(err,data)=>{
            if(!err){
              if(data.length>0){
                   resp.send(data)
              }
            }
     })
    
         
       },
       upVideoImg(req,resp){
        let pathname=req.file.filename
        let userPathName= pathname.split(".")[0]
        dbPool.connect("INSERT INTO movies(title,imgSrc) VALUES(?,?)",[userPathName,userPathName],(err,data)=>{
            if(!err){
              resp.send(userPathName)
                
            }                          
 })
        //  console.log(userPathName)
       }
//算法
,  getRateData(req,resp){
       dbPool.connect("select * from rate",[],(err,data)=>{
                 if(!err){
                  if(data.length>0){
                  
                    resp.send(data)
                     
                  }   
                       
                 }
       })
       
},
like(req,resp){
       let arr=[]
      dbPool.connect("select * from movies",[],(err,data)=>{
                    if(!err){
                        //  console.log(data)
                        for(let i=0;i<req.query.goodsId.length;i++){
                             for(let j=0;j<data.length;j++){
                                if(req.query.goodsId[i].goodsId==data[j].id){
                                       arr.push(data[j])
                                }
                             }     
                              
                        } 
                       resp.send(arr)
                    }
      })

   
},

upVideo(req,resp){
    let pathname=req.file.filename
    let userPathName= pathname.split(".")[0]
           


    dbPool.connect("UPDATE movies SET `videoSrc`=? where imgSrc=?",[userPathName,req.body.imgSrc],(err,data)=>{
           if(!err){
             console.log("更新成功")
           }
    })

   
 
    
 


    
}
        


}

module.exports=movieController